package com.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.web.entity.Token;
import com.web.service.ITokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@EnableScheduling //开启定时任务
@EnableAsync //开启多线程
public class TimedTasks {

    @Autowired private ITokenService tokenService;

    @Async
    @Scheduled(fixedDelay = 1000 * 60 * 30)
    public void oneTimedTasks(){
        System.err.println("第一个定时任务开始时间" + System.currentTimeMillis());
        this.oneFunction();
    }

    private void oneFunction(){
        long time = 1000 * 60 * 30;
        Date now = new Date(System.currentTimeMillis() - time);
        Wrapper<Token> wrapper = new EntityWrapper<>();
        wrapper.le("time", now);
        this.tokenService.delete(wrapper);
    }
}
